home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 17 controls / databindingdemo / datagridform.vb < prev    next >
Text File  |  2002-03-16  |  11KB  |  263 lines

  1. Imports System.Data
  2. Imports System.Data.OleDb
  3.  
  4. Public Class DataGridForm
  5.     Inherits System.Windows.Forms.Form
  6.  
  7. #Region " Windows Form Designer generated code "
  8.  
  9.     Public Sub New()
  10.         MyBase.New()
  11.  
  12.         'This call is required by the Windows Form Designer.
  13.         InitializeComponent()
  14.  
  15.         'Add any initialization after the InitializeComponent() call
  16.  
  17.     End Sub
  18.  
  19.     'Form overrides dispose to clean up the component list.
  20.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  21.         If disposing Then
  22.             If Not (components Is Nothing) Then
  23.                 components.Dispose()
  24.             End If
  25.         End If
  26.         MyBase.Dispose(disposing)
  27.     End Sub
  28.     Friend WithEvents Label1 As System.Windows.Forms.Label
  29.     Friend WithEvents btnFirst As System.Windows.Forms.Button
  30.     Friend WithEvents lblRecord As System.Windows.Forms.Label
  31.     Friend WithEvents btnNext As System.Windows.Forms.Button
  32.     Friend WithEvents btnLast As System.Windows.Forms.Button
  33.     Friend WithEvents btnPrevious As System.Windows.Forms.Button
  34.     Friend WithEvents txtName As System.Windows.Forms.TextBox
  35.     Friend WithEvents Label2 As System.Windows.Forms.Label
  36.     Friend WithEvents txtState As System.Windows.Forms.TextBox
  37.     Friend WithEvents Label3 As System.Windows.Forms.Label
  38.     Friend WithEvents txtCity As System.Windows.Forms.TextBox
  39.     Friend WithEvents grdTitles As System.Windows.Forms.DataGrid
  40.         
  41.     'Required by the Windows Form Designer
  42.     Private components As System.ComponentModel.Container
  43.  
  44.     'NOTE: The following procedure is required by the Windows Form Designer
  45.     'It can be modified using the Windows Form Designer.  
  46.     'Do not modify it using the code editor.
  47.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  48.         Me.btnLast = New System.Windows.Forms.Button()
  49.         Me.txtCity = New System.Windows.Forms.TextBox()
  50.         Me.Label1 = New System.Windows.Forms.Label()
  51.         Me.btnPrevious = New System.Windows.Forms.Button()
  52.         Me.Label3 = New System.Windows.Forms.Label()
  53.         Me.btnFirst = New System.Windows.Forms.Button()
  54.         Me.btnNext = New System.Windows.Forms.Button()
  55.         Me.txtState = New System.Windows.Forms.TextBox()
  56.         Me.lblRecord = New System.Windows.Forms.Label()
  57.         Me.Label2 = New System.Windows.Forms.Label()
  58.         Me.txtName = New System.Windows.Forms.TextBox()
  59.         Me.grdTitles = New System.Windows.Forms.DataGrid()
  60.         CType(Me.grdTitles, System.ComponentModel.ISupportInitialize).BeginInit()
  61.         Me.SuspendLayout()
  62.         '
  63.         'btnLast
  64.         '
  65.         Me.btnLast.Location = New System.Drawing.Point(344, 80)
  66.         Me.btnLast.Name = "btnLast"
  67.         Me.btnLast.Size = New System.Drawing.Size(48, 32)
  68.         Me.btnLast.TabIndex = 11
  69.         Me.btnLast.Text = ">>"
  70.         '
  71.         'txtCity
  72.         '
  73.         Me.txtCity.Location = New System.Drawing.Point(208, 40)
  74.         Me.txtCity.Name = "txtCity"
  75.         Me.txtCity.Size = New System.Drawing.Size(160, 24)
  76.         Me.txtCity.TabIndex = 0
  77.         Me.txtCity.Text = ""
  78.         '
  79.         'Label1
  80.         '
  81.         Me.Label1.Location = New System.Drawing.Point(24, 16)
  82.         Me.Label1.Name = "Label1"
  83.         Me.Label1.Size = New System.Drawing.Size(152, 16)
  84.         Me.Label1.TabIndex = 1
  85.         Me.Label1.Text = "Name"
  86.         '
  87.         'btnPrevious
  88.         '
  89.         Me.btnPrevious.Location = New System.Drawing.Point(160, 80)
  90.         Me.btnPrevious.Name = "btnPrevious"
  91.         Me.btnPrevious.Size = New System.Drawing.Size(48, 32)
  92.         Me.btnPrevious.TabIndex = 8
  93.         Me.btnPrevious.Text = "<"
  94.         '
  95.         'Label3
  96.         '
  97.         Me.Label3.Location = New System.Drawing.Point(208, 16)
  98.         Me.Label3.Name = "Label3"
  99.         Me.Label3.Size = New System.Drawing.Size(152, 16)
  100.         Me.Label3.TabIndex = 1
  101.         Me.Label3.Text = "City"
  102.         '
  103.         'btnFirst
  104.         '
  105.         Me.btnFirst.Location = New System.Drawing.Point(112, 80)
  106.         Me.btnFirst.Name = "btnFirst"
  107.         Me.btnFirst.Size = New System.Drawing.Size(48, 32)
  108.         Me.btnFirst.TabIndex = 7
  109.         Me.btnFirst.Text = "<<"
  110.         '
  111.         'btnNext
  112.         '
  113.         Me.btnNext.Location = New System.Drawing.Point(296, 80)
  114.         Me.btnNext.Name = "btnNext"
  115.         Me.btnNext.Size = New System.Drawing.Size(48, 32)
  116.         Me.btnNext.TabIndex = 10
  117.         Me.btnNext.Text = ">"
  118.         '
  119.         'txtState
  120.         '
  121.         Me.txtState.Location = New System.Drawing.Point(376, 40)
  122.         Me.txtState.Name = "txtState"
  123.         Me.txtState.Size = New System.Drawing.Size(96, 24)
  124.         Me.txtState.TabIndex = 0
  125.         Me.txtState.Text = ""
  126.         '
  127.         'lblRecord
  128.         '
  129.         Me.lblRecord.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  130.         Me.lblRecord.Location = New System.Drawing.Point(208, 80)
  131.         Me.lblRecord.Name = "lblRecord"
  132.         Me.lblRecord.Size = New System.Drawing.Size(88, 32)
  133.         Me.lblRecord.TabIndex = 9
  134.         Me.lblRecord.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
  135.         '
  136.         'Label2
  137.         '
  138.         Me.Label2.Location = New System.Drawing.Point(376, 16)
  139.         Me.Label2.Name = "Label2"
  140.         Me.Label2.Size = New System.Drawing.Size(56, 16)
  141.         Me.Label2.TabIndex = 1
  142.         Me.Label2.Text = "State"
  143.         '
  144.         'txtName
  145.         '
  146.         Me.txtName.Location = New System.Drawing.Point(24, 40)
  147.         Me.txtName.Name = "txtName"
  148.         Me.txtName.Size = New System.Drawing.Size(168, 24)
  149.         Me.txtName.TabIndex = 0
  150.         Me.txtName.Text = ""
  151.         '
  152.         'grdTitles
  153.         '
  154.         Me.grdTitles.AlternatingBackColor = System.Drawing.Color.LightGray
  155.         Me.grdTitles.BackColor = System.Drawing.Color.DarkGray
  156.         Me.grdTitles.BackgroundColor = System.Drawing.Color.Silver
  157.         Me.grdTitles.CaptionBackColor = System.Drawing.Color.White
  158.         Me.grdTitles.CaptionFont = New System.Drawing.Font("Verdana", 10!)
  159.         Me.grdTitles.CaptionForeColor = System.Drawing.Color.Navy
  160.         Me.grdTitles.CaptionText = "Titles"
  161.         Me.grdTitles.DataMember = ""
  162.         Me.grdTitles.ForeColor = System.Drawing.Color.Black
  163.         Me.grdTitles.GridLineColor = System.Drawing.Color.Black
  164.         Me.grdTitles.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None
  165.         Me.grdTitles.HeaderBackColor = System.Drawing.Color.Silver
  166.         Me.grdTitles.HeaderForeColor = System.Drawing.Color.Black
  167.         Me.grdTitles.LinkColor = System.Drawing.Color.Navy
  168.         Me.grdTitles.Location = New System.Drawing.Point(19, 136)
  169.         Me.grdTitles.Name = "grdTitles"
  170.         Me.grdTitles.ParentRowsBackColor = System.Drawing.Color.White
  171.         Me.grdTitles.ParentRowsForeColor = System.Drawing.Color.Black
  172.         Me.grdTitles.SelectionBackColor = System.Drawing.Color.Navy
  173.         Me.grdTitles.SelectionForeColor = System.Drawing.Color.White
  174.         Me.grdTitles.Size = New System.Drawing.Size(448, 192)
  175.         Me.grdTitles.TabIndex = 12
  176.         '
  177.         'DataGridForm
  178.         '
  179.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  180.         Me.ClientSize = New System.Drawing.Size(480, 341)
  181.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.txtCity, Me.Label2, Me.txtState, Me.btnFirst, Me.lblRecord, Me.btnNext, Me.btnLast, Me.btnPrevious, Me.Label1, Me.txtName, Me.grdTitles})
  182.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  183.         Me.Name = "DataGridForm"
  184.         Me.Text = "AdoNetBindingForm"
  185.         CType(Me.grdTitles, System.ComponentModel.ISupportInitialize).EndInit()
  186.         Me.ResumeLayout(False)
  187.  
  188.     End Sub
  189.  
  190. #End Region
  191.  
  192.     ' initiate binding when the form loads
  193.  
  194.     Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  195.         CreateDataSet()
  196.         InitializeBinding()
  197.     End Sub
  198.  
  199.     Dim dsBiblio As New DataSet()
  200.     Dim bmb As BindingManagerBase
  201.  
  202.     ' create a DataSet with the  Publishers table
  203.  
  204.     Sub CreateDataSet()
  205.         ' Open a connection to the Biblio.mdb database.
  206.         Dim cn As New OleDbConnection(BiblioConnString)
  207.         cn.Open()
  208.  
  209.         ' Create a data adapter for the Publisher table.
  210.         Dim daPublishers As New OleDbDataAdapter("SELECT * FROM Publishers", cn)
  211.         ' Fill the DataSet object with Publishers data.
  212.         daPublishers.Fill(dsBiblio, "Publishers")
  213.  
  214.         ' Create a data adapter for the Titles table.
  215.         Dim daTitles As New OleDbDataAdapter("SELECT * FROM Titles", cn)
  216.         ' Fill the DataSet object with Titles data.
  217.         daTitles.Fill(dsBiblio, "Titles")
  218.  
  219.         ' Create a Relation between the two table
  220.         dsBiblio.Relations.Add("PubTitles", dsBiblio.Tables("Publishers").Columns("PubID"), dsBiblio.Tables("Titles").Columns("PubId"))
  221.  
  222.         ' Close the connection
  223.         cn.Close()
  224.     End Sub
  225.  
  226.     ' connect all fields to the data source 
  227.  
  228.     Sub InitializeBinding()
  229.         ' Bind fields to database fields.
  230.         txtName.DataBindings.Add("Text", dsBiblio, "Publishers.Name")
  231.         txtCity.DataBindings.Add("Text", dsBiblio, "Publishers.City")
  232.         txtState.DataBindings.Add("Text", dsBiblio, "Publishers.State")
  233.         ' Bind the Data Grid.
  234.         grdTitles.DataSource = dsBiblio
  235.         grdTitles.DataMember = "Publishers.PubTitles"
  236.  
  237.         ' get a reference to the BindingManagerBase object.
  238.         bmb = Me.BindingContext(dsBiblio, "Publishers")
  239.  
  240.         ' Force a refresh.
  241.         bmb.Position = bmb.Count
  242.         bmb.Position = 0
  243.     End Sub
  244.  
  245.     ' navigational buttons
  246.  
  247.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
  248.         bmb.Position = 0
  249.     End Sub
  250.  
  251.     Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
  252.         bmb.Position -= 1
  253.     End Sub
  254.  
  255.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  256.         bmb.Position += 1
  257.     End Sub
  258.  
  259.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
  260.         bmb.Position = bmb.Count
  261.     End Sub
  262. End Class
  263.